package com.aote.expression.paramprocessor;

import java.util.Map;

import com.aote.expression.Param;
import com.browsesoft.dbtools.DBTools;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.user.User;

public class CreateNumberProcessor implements ParamProcessor {

	public synchronized String process(Param param, Map attribures,
			User loginUser, HTMLBasicComponent component) {
		
			Map map = param.getParams();
			String type = (String) map.get("type");
			if (attribures.containsKey(type)) {
				type = (String) attribures.get(type);
				return getNumber(type);
			}
			return "";
		
	}

	private String getNumber(String name) {
		String result = "";
		String sql = " select a.f_qishicard "
				+ "from t_cardreset a,t_cardresetwatch b "
				+ "where a.id=b.parentid and b.f_gaswatchbrand='" + name + "'";
		try {
			String[][] str = DBTools.executeQueryWithTableHead(sql);
			if (str.length > 1) {
				result = str[1][0];
				result = Long.parseLong(result) + 1 + "";
			}
			// 保存卡号
			savenumber(name, result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 保存卡号
	 */
	private void savenumber(String name, String newcardid) {
		String sql = "update t_cardreset set f_qishicard='"
				+ newcardid
				+ "' where id=(select parentid from t_cardresetwatch where f_gaswatchbrand='"
				+ name + "')";
		try {
			DBTools.excuteUpdate(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
